<?php

class Sistema_MenuController extends App_Controller_Action {
    
    private $_table     = 'MENU';
    private $_pk        = 'MEN_ID';
    private $_order_col = 'MEN_ORD';
    private $_order     = 'ASC';
    private $_sta_col   = '';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'MEN_FILE'; 
    public  $dir = 'public/upload/menu/';
    
    public function init()
    {
        $this->obj = new Sistema_Model_DbTable_Menu();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Menu();
    }

    public function indexAction() {
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $this->form;
        
        if ($this->getRequest()->isPost()) {
            try {
                $formData = $this->getRequest()->getPost();
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue('MEN_ID');

                    //$returnImg = imagem::upload($this->_file_col,$this->dir,$id);

                    $data = array(
                        'MEN_NOME' => $this->form->getValue('MEN_NOME'),
                        'MEN_ORD' => $this->form->getValue('MEN_ORD'),
                        'MEN_STA' => $this->form->getValue('MEN_STA',0),
                        //'MEN_CSS' => $this->form->getValue('MEN_CSS'),
                    );
                    
                    if ($id == 0){
                        $this->obj->insert($data);
                        $id =  $this->obj->getAdapter()->lastInsertId();                    
                        //imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else
                        $this->obj->update($data, 'MEN_ID = ' . $id);
                    
                    /*
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '27', '20');
                    }
                    */
                    
                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $this->form->populate($this->obj->fetchRow('MEN_ID = ' . $id)->toArray());
            }
        }
        
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $this->obj->delete('MEN_ID = '.(int)$id);
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $this->form->populate($this->obj->fetchRow('MEN_ID = ' . $id)->toArray());
        }
    }

}

